package com.bestvike.stone.spring.shiro;

import com.bestvike.stone.spring.shiro.annotation.resolver.AnnotationResolver;
import com.bestvike.stone.spring.shiro.cache.CacheManager;
import com.bestvike.stone.spring.shiro.mgt.DefaultSecurityManager;
import com.bestvike.stone.spring.shiro.mgt.SecurityManager;
import com.bestvike.stone.spring.shiro.realm.Realm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * Shiro 自动配置
 */
@Configuration
public class ShiroAutoConfiguration {
    /**
     * 安全管理器
     */
    @Bean
    SecurityManager securityManager(@Autowired(required = false) AnnotationResolver annotationResolver,
                                    @Autowired(required = false) CacheManager cacheManager,
                                    @Autowired(required = false) List<Realm> realms) {
        final DefaultSecurityManager securityManager = new DefaultSecurityManager();
        securityManager.setAnnotationResolver(annotationResolver);
        securityManager.setCacheManager(cacheManager);
        securityManager.setRealms(realms);
        SecurityUtils.setSecurityManager(securityManager);
        return securityManager;
    }
}
